package com.ruanko.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.DBUtil.SQLHelper;
import com.ruanko.dao.UserInfoDao;
import com.ruanko.entity.UserInfoBean;

public class UserInfoDaoImpl implements UserInfoDao {

	private SQLHelper sqlHelper = new SQLHelper();
	private ArrayList<UserInfoBean> userInfoList = new ArrayList<UserInfoBean>(); // 鐢ㄦ埛淇℃伅鍒楄〃

	/**
	 * 根据账号，查询并返回用户基本信息
	 * 
	 * @param userID
	 * @return
	 */
	public UserInfoBean getUserResult(String userID) {

		String sql = "select userid,password from userinfo where userid = ?";
		String[] parameters = new String[1];
		parameters[0] = userID;
		UserInfoBean userInfoBean = new UserInfoBean();
		try {
			ArrayList<Object> templList = sqlHelper.executeQuery(sql,
					parameters);

			if (!templList.isEmpty()) {

				Object[] object = (Object[]) templList.get(0);
				userInfoBean.setUserId(object[0].toString());
				userInfoBean.setPwd(object[1].toString());
			}

		} catch (Exception e) {
			// TODO: handle exception
		}
		return userInfoBean;
	}

	/**
	 * 查询并返回用户信息列表
	 * @return
	 */
	public List<UserInfoBean> getUserResult() {
		if(userInfoList!=null){
    		userInfoList.clear();
    	}
		String sql = "SELECT * from userinfo";
		String[] param = {};
		try {
			ArrayList<Object> templList = sqlHelper.executeQuery(sql, param);
			if (!templList.isEmpty()) {
				for (int i = 0; i < templList.size(); i++) {
					Object[] objects = (Object[]) templList.get(i);
					UserInfoBean userInfoBean = new UserInfoBean();
					userInfoBean.setUserId(objects[0].toString());
					userInfoBean.setPwd(objects[1].toString());
					userInfoBean.setEmail(objects[2].toString());
					userInfoList.add(userInfoBean);
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return userInfoList;
	}
	/**
	 *分页查询显示
	 * (non-Javadoc)
	 * @see com.ruanko.dao.UserInfoDao#queryByPage(int, int)
	 */
	public List<UserInfoBean> queryByPage(int pageSize, int pageNow) {  
	    try {
	    	if(userInfoList!=null){
	    		userInfoList.clear();
	    	}
	    	if(pageSize>0 && pageNow>0){
    			String sql = "select * from userinfo order by userid limit "+(pageNow*pageSize-pageSize)+","+pageSize;
    			String[] param = {};
    			ArrayList<Object> templList = sqlHelper.executeQuery(sql, param);
    			if (!templList.isEmpty()) {
    				for (int i = 0; i < templList.size(); i++) {
    					Object[] objects = (Object[]) templList.get(i);
    					UserInfoBean userInfoBean = new UserInfoBean();
    					userInfoBean.setUserId(objects[0].toString());
    					userInfoBean.setPwd(objects[1].toString());
    					userInfoBean.setEmail(objects[2].toString());
    					userInfoList.add(userInfoBean);
    				}
    			}
	    	}
	    	
		} catch (Exception e) {
			e.printStackTrace() ; 
		}
	    return userInfoList;
	}

	/**
	 * 插入用户基本信息列表
	 * @param userInfoList
	 * @return
	 */
	public boolean insertUserInfo(List<UserInfoBean> userInfoList) {

		boolean b = false;

		try {
			String sql = "insert into userinfo values(?,?,?)";
			String[] parms = new String[userInfoList.size() * 3];
			int parmLenght = 0;

			if (!userInfoList.isEmpty()) {

				for (int i = 0; i < userInfoList.size(); i++) {
					if (i != 0) {
						sql = sql + ",(?,?,?)";
					}
					parms[parmLenght++] = userInfoList.get(i).getUserId();
					parms[parmLenght++] = userInfoList.get(i).getPwd();
					parms[parmLenght++] = userInfoList.get(i).getEmail();
				}
				b = sqlHelper.executeUpdate(sql, parms);
			}
		} catch (Exception e) {
		}

		return b;
	}

	/**
	 * 批量删除用户信息
	 * @param userInfoList
	 * @return
	 */
	public boolean deleteUserInfo(List<UserInfoBean> userInfoList) {
		boolean b = false;
		String[] sqls = new String[userInfoList.size()];
		String[][] params = new String[userInfoList.size()][1];
		if (!userInfoList.isEmpty()) {
			for (int i = 0; i < userInfoList.size(); i++) {
				sqls[i] = "delete from userinfo where userid = ?";
				params[i][0] = userInfoList.get(i).getUserId();
			}
			SQLHelper.executeUpdate2(sqls, params);
			b = true;
		}
		return b;
	}

	/**
	 * 批量更新用户信息
	 * @param userInfoList
	 * @return
	 */
	public boolean updateUserInfo(List<UserInfoBean> userInfoList) {
		boolean b = false;
		String[] sqls = new String[userInfoList.size()];
		String[][] params = new String[userInfoList.size()][4];
		if (!userInfoList.isEmpty()) {
			for (int i = 0; i < userInfoList.size(); i++) {
				sqls[i] = "update userinfo set userid=?,password=?,email=? where userid =?";
				params[i][0] = userInfoList.get(i).getUserId();
				params[i][1] = userInfoList.get(i).getPwd();
				params[i][2] = userInfoList.get(i).getEmail();
				params[i][3] = userInfoList.get(i).getUserId();
			}
			SQLHelper.executeUpdate2(sqls, params);
			b = true;
		}
		return b;
	}

	
}
